package org.finalministry.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.io.FileUtils;

public class ImportCountries {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {

		File file = new File("data/countries.txt");
		List<String> lines = FileUtils.readLines(file, "UTF-8");

		File importCountriesFile = new File("sql/importCountries.sql");
		BufferedWriter out = new BufferedWriter(new FileWriter(importCountriesFile));

		int id = 1;
		boolean firstline = true;

		for (Iterator<String> iterator = lines.iterator(); iterator.hasNext();) {

			String line = iterator.next();

			if (firstline) {
				firstline = false;
				continue;
			}

			System.out.println(line);
			String parts[] = line.split("\t");

			out.write("INSERT INTO COUNTRY (ID,ISOCODE2,ISOCODE3,ENGLISHNAME,ORIGINALNAME) VALUES(" + id + ",'" + parts[0] + "','" + parts[1]
					+ "','" + parts[4] + "','');");
			out.newLine();

			id += 1;
		}

		out.close();
	}

}
